﻿using System;
using System.Windows;
using System.Windows.Controls;
using Silvery.Loader;
using Silvery.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Silvery.Role.Standard
{
    public class StandardRolePartLoader : SpiritLoader
    {
        public StandardRolePartLoader()
        {

        }
        public StandardRolePartLoader(Uri uri, ISpirit spirit = null)
            : base(uri)
        {            
        }

        protected override ISpiritData Create(IZIPLoader data)
        {
            NSpiritData spiritdata = new NSpiritData();
            var s = base.Create(data);
            spiritdata.FramesInfo = s.FramesInfo;
            
            foreach (var aniname in s.AnimatNamesList)
            {
                var frameslist = s.FindAnimat(aniname);
                var ispan = frameslist.Count / 5;//五个方向
                for (int i = 0; i < 5; i++)
                {
                    for (int j = ispan * i; j < ispan * i + ispan; j++)
                    {
                        var temp = (RoleDirection)i;
                        var dir = RoleDirectionHelper.GetMirrorDir(temp);
                        if (dir != temp)
                        {
                            var imagesource = frameslist[j];
                            Image img = new Image();
                            img.Source = imagesource.Source;
                            img.RenderTransformOrigin = new Point(0.5, 0.5);
                            img.RenderTransform = new ScaleTransform() { ScaleX = -1 };
                            spiritdata.AddFrame(aniname + dir, img);
                            var pt = (Point)imagesource.Tag;
                            if (pt != null)
                            {
                                var ptt = new Point(-pt.X - (imagesource.Source as BitmapImage).PixelWidth, pt.Y);
                                img.Tag = ptt;
                            }
                        }
                        spiritdata.AddFrame(aniname + temp, frameslist[j]);
                    }
                }
                
            }
            s.IsDisposeClear = false;
            s.Dispose();
            return spiritdata;
        }
        
    }
}
